﻿

var statusArray = new Array();

function setLeftClick(task, value)
{
	var already = false;
	for (var i=0; i<statusArray.length; i++)
	{
		if (statusArray[i].task == task)
		{
			statusArray[i].leftClick = value;
			already = true;
			break;
		}
	}
	if (!already)
	{
		var obj = new Object();
		obj.task = task;
		obj.leftClick = value;
		statusArray[statusArray.length] = obj;
	}
}

function getLeftClick(task)
{
	for (var i=0; i<statusArray.length; i++)
	{
		if (statusArray[i].task == task)
		{
			return statusArray[i].leftClick;
		}
	}
	return undefined;
}

function setMouseStartX(task, value)
{
	var already = false;
	for (var i=0; i<statusArray.length; i++)
	{
		if (statusArray[i].task == task)
		{
			statusArray[i].mouseStartX = value;
			already = true;
			break;
		}
	}
	if (!already)
	{
		var obj = new Object();
		obj.task = task;
		obj.mouseStartX = value;
		statusArray[statusArray.length] = obj;
	}
}

function getMouseStartX(task)
{
	for (var i=0; i<statusArray.length; i++)
	{
		if (statusArray[i].task == task)
		{
			return statusArray[i].mouseStartX;
		}
	}
	return undefined;
}

function moveOnTask(task, e)
{
	var adjuster = $(task).find(".statusAdjuster")[0];
	var label = $(task).find(".statusLabel");
	var line = $(task).find(".statusLine")[0];
	var evt = window.event || e;
	var leftClick = getLeftClick(task);
	if (leftClick)
	{
		var curX = parseInt(adjuster.style.left);
		if (isNaN(curX)) curX = 0;
		var mouseStartX = getMouseStartX(task);
		
		var newX = curX + evt.clientX - mouseStartX;
		if ((newX < 0) || (newX > line.clientWidth)) {}
		else
		{
			adjuster.style.left = newX + "px";
			setMouseStartX(task, evt.clientX);
			var percent = newX / line.clientWidth * 100;
			label.val(percent.toFixed(0) + "%");
		}
	}//alert("move");
	$(task).find(".bar").eq(0).css("visibility", "visible");
}

function outOfTask(task, e)
{
    $(task).find(".bar").css("visibility", "hidden");
}

function downOnAdjuster(adjuster, e)
{
	var evt = window.event || e;
	if (!evt.which)
	{//ie
		setLeftClick(adjuster.parentNode, (evt.button == 1));
	}
	else
	{
		setLeftClick(adjuster.parentNode, (evt.button == 0));
	}
	setMouseStartX(adjuster.parentNode, evt.clientX);
}

function upOnTask(task, e)
{
	setLeftClick(task, false);
}


function clickOnLine(line, e)
{
	var adjuster = $(line.parentNode).find(".statusAdjuster")[0];
	var label = $(line.parentNode).find(".statusLabel");
	var evt = window.event || e;
	var px = evt.offsetX;
	if (px == undefined)
	{//Firefox
		px = evt.layerX;
	}
	adjuster.style.left = px + "px";
	var percent = px / line.clientWidth * 100;
	label.val(percent.toFixed(0) + "%");
}


function addTask(button)
{
    var argsVariable = new Object();
    var answer = window.showModalDialog("/Task/AddTask.aspx",
        argsVariable, 
        "dialogWidth:500px; dialogHeight:500px; dialogLeft:250px; dialogTop:100px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
}

function editTask(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var argsVariable = new Object();
    var answer = window.showModalDialog("/Task/EditTask.aspx?taskId=" + idInput.val(),
        argsVariable, 
        "dialogWidth:500px; dialogHeight:500px; dialogLeft:250px; dialogTop:100px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
}

function removeTask(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var argsVariable = new Object();
    var answer = window.showModalDialog("/Task/RemoveTask.aspx?taskId=" + idInput.val(),
        argsVariable, 
        "dialogWidth:500px; dialogHeight:500px; dialogLeft:250px; dialogTop:100px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
}

function selectMonth()
{
    var month = $("input[name=month]").val();
    var year = $("input[name=year]").val();
    window.location.href = "/Task/ManageTask.aspx?year=" + year + "&month=" + month;
}

function previousMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    month--;
    if (month < 1)
    {
        year--;
        month = 12;
    }
    window.location.href = "/Task/ManageTask.aspx?year=" + year + "&month=" + month;
}

function nextMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    month++;
    if (month > 12)
    {
        year++;
        month = 1;
    }
    window.location.href = "/Task/ManageTask.aspx?year=" + year + "&month=" + month;
}

function previousYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    year--;
    window.location.href = "/Task/ManageTask.aspx?year=" + year + "&month=" + month;
}

function nextYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    year++;
    window.location.href = "/Task/ManageTask.aspx?year=" + year + "&month=" + month;
}